package com.ysg.utils;

import android.text.TextUtils;

import com.ysg.base.BaseApplication;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 */
public class YStringUtil {
    public static final String[] CONSTELLATION_ARR = { "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };
    public static final int[] CONSTELLATION_EDGE_DAY = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };
    /**
     * 如果该字符串为空 则return掉
     */
    public static boolean isEmpty(String s) {
        if (TextUtils.isEmpty(s)) {
            return true;
        }
        if (TextUtils.isEmpty(s.trim())) {
            return true;
        }
        if ("null".equals(s.trim())) {
            return true;
        }
        if ("NULL".equals(s.trim())) {
            return true;
        }
        if ("".equals(s.trim())) {
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(String s){
        return !isEmpty(s);
    }

    /**
     * 是否相等
     */
    public static boolean eq(Object o1, Object o2){
        if (o1 == null){
            return false;
        }
        if (o2 == null){
            return false;
        }
        return String.valueOf(o1).equalsIgnoreCase(String.valueOf(o2));
    }

    /**
     * 是否相等
     */
    public static boolean noEq(Object o1, Object o2){
        return !eq( o1,  o2);
    }

    /**
     * 去掉尾部逗号
     */
    public static String clearEndComma(String s){
        if(isEmpty(s)){
            return "";
        }else{
            if(s.endsWith(",")){
                s = s.substring(0, s.length()-1);
            }
        }
        return s;
    }

    /**
     * 去掉尾部
     */
    public static String clearEnd(String s, String pattern){
        if(isEmpty(s)){
            return "";
        }else{
            if(s.endsWith(pattern)){
                s = s.substring(0, s.length() - pattern.length());
            }
        }
        return s;
    }
    /**
     * 有效数字格式化，保留两位有效数字
     */
    public static String decimalFormat(double dou, boolean saveLastNumber) {
        DecimalFormat df = new DecimalFormat("#.00");
        if (saveLastNumber) {
            return new BigDecimal(df.format(dou)).toPlainString();
        } else {
            return new BigDecimal(df.format(dou)).stripTrailingZeros().toPlainString();
        }
    }

    public static boolean isDefaultAddress(String isDefault){
        if (eq("1", isDefault)){
            return true;
        }else{
            return false;
        }
    }

    public static String showDefaultZero(String s){
        if (isEmpty(s)){
            s = "0";
        }
        return s;
    }

    public static String getIds(List<String> list){
        String ids = "";
        if (YCollectionUtil.isNotEmpty(list)){
            for(int i = 0; i < list.size(); i++){
                if (i == 0){
                    ids = list.get(i);
                }else{
                    ids += "," + list.get(i);
                }
            }
        }
        return ids;
    }

    public static boolean contains(String parent, String s){
        if (isNotEmpty(parent) && parent.contains(s)){
            return true;
        }
        return false;
    }

    /**
     * 隐藏银行卡号中间的字符串（使用*号），显示前四后四
     */
    public static String hideCardNo(String cardNo, String symbol) {
        int length = cardNo.length();
        int beforeLength = 4;
        int afterLength = 4;
        //替换字符串，当前使用“*”
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            if (i < beforeLength || i >= (length - afterLength)) {
                sb.append(cardNo.charAt(i));
            } else {
                sb.append(symbol);
            }
        }
        return sb.toString();
    }

    /**
     * 是否是数字
     */
    public static boolean isNumeric(Object o){
        if (o == null){
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(o.toString()).matches();
    }

    /**
     * 是否是小秘书
     */
    public static boolean isAdmin(String admin){
        return eq("administrator", admin);
    }

    /**
     * 去除小数点后无用的0
     */
    public static String removeZero(String s) {
        if (isNotEmpty(s) && s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉后面无用的零
            s = s.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return s;
    }

    /**
     * 是否男女默认头像
     */
    public static boolean isAvatarDefault(String url){
        return YStringUtil.eq(url, "http://osszhiyuhn1.ptszyxx.com/default/nang.png")
                || YStringUtil.isEmpty(url)
                || YStringUtil.eq(url, "http://osszhiyuhn1.ptszyxx.com/default/nv.png");
    }

    /**
     * 不够补零 zeroFill
     */
    public static String zeroFill(String src) {
        if (null == src) {
            return null;
        }
        String FORMAT_STRING = "00000";
        int delta = FORMAT_STRING.length() - src.length();
        if (delta <= 0) {
            return src;
        }
        return FORMAT_STRING.substring(0, delta) + src;
    }

    /**
     * R.string + 字符串
     */
    public static String getResIdAndStr(int resId, Object s){
        return BaseApplication.getContext().getResources().getString(resId) + s;
    }

    /**
     * R.string + 字符串
     */
    public static String getResIdByFormat(int resId, String s){
        String str = String.format(BaseApplication.getContext().getString(resId), s);
        return str;
    }

    /**
     * 字符串（逗号拼接的）——》List
     */
    public static List<String> getListByComma(String s){
        List<String> list = new ArrayList<>();
        if (YStringUtil.isNotEmpty(s)){
            String[] split = s.split(",");
            for (String s1: split){
                list.add(s1);
            }
        }
        return list;
    }

    /**
     * 根据日期获取星座
     */
    public static String getConstellation(String dateStr){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);
            if (date != null) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);
                if (day < CONSTELLATION_EDGE_DAY[month]) {
                    month = month - 1;
                }
                if (month >= 0) {
                    return CONSTELLATION_ARR[month];
                }
                return CONSTELLATION_ARR[11];
            }
        }catch (Exception e){
        }
        return "";
    }

    /**
     * 获取年份：如85；00
     */
    public static String getYearTime(String dateStr){
        String year = "";
        if (YStringUtil.isNotEmpty(dateStr)){
            year = dateStr.substring(2, 4);
        }
        return year;
    }



}
